/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.grasscoverage.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.longview.io.common.persistence.Page;
import com.longview.io.common.service.CrudService;
import com.longview.io.modules.grassbelt.entity.SysGrassBelt;
import com.longview.io.modules.grasscoverage.entity.GrassCoverage;
import com.longview.io.modules.grasscoverage.dao.GrassCoverageDao;

/**
 * 草场盖度Service
 * @author zf
 * @version 2018-05-18
 */
@Service
@Transactional(readOnly = true)
public class GrassCoverageService extends CrudService<GrassCoverageDao, GrassCoverage> {

	public GrassCoverage get(String id) {
		return super.get(id);
	}
	
	public List<GrassCoverage> findList(GrassCoverage grassCoverage) {
		return super.findList(grassCoverage);
	}
	
	public Page<GrassCoverage> findPage(Page<GrassCoverage> page, GrassCoverage grassCoverage) {
		return super.findPage(page, grassCoverage);
	}
	
	@Transactional(readOnly = false)
	public void save(GrassCoverage grassCoverage) {
		super.save(grassCoverage);
	}
	
	@Transactional(readOnly = false)
	public void delete(GrassCoverage grassCoverage) {
		super.delete(grassCoverage);
	}
	//通过采用带id获取草原
	public List<GrassCoverage> getGrassCoverageByBelt(GrassCoverage grassCoverage)
	{
		return dao.getGrassCoverageByBelt(grassCoverage);
	}
	/***
	 * 
	 * @param grassbelt
	 * @param avg
	 * @param beltMonthxishu   草原带盖度草量月系数
	 * @param curMonthxishu    当月系数
	 * @return
	 */
	//根据草地比例计算最接近的草量
	public double getNearGrass(SysGrassBelt grassbelt,String avg,double beltMonthxishu,double curMonthxishu)
	{
		double avgJs = Double.parseDouble(avg) * 100;
		//如果草地小于10%那么直接输出不适合放牧
/*		if(avgJs < 0.1)
			return 0;*/
		GrassCoverage grass = new GrassCoverage();
		grass.setGrassbeltId(grassbelt);
		List<GrassCoverage> list = getGrassCoverageByBelt(grass);
		if(list.size() == 0)
			return 0;
	    int targetindex = -1;  
	    int left = -1, right = -1;   
	    double chanliang = 0;
	    for(int i=0;i<list.size();i++)
	    {
	    	GrassCoverage midGrassCoverage = (GrassCoverage) list.get(i);  
	    	double avgs = Double.parseDouble(midGrassCoverage.getCoverage());
	    	if(avgJs < avgs)
	    	{
	    		//chanliang = (Double.parseDouble(midGrassCoverage.getProduction())/avgs)*avgJs;
	    		right = i;
	    		break;
	    	}
	    	else if(avgJs == avgs)
	    	{
	    		targetindex = i;
	    		break;
	    		//chanliang = Double.parseDouble(midGrassCoverage.getProduction());	    		
	    	}

	    	else if(avgJs > avgs)
	    	{
	    		left = i;
	    		continue;
	    	}
	    		
	    }
	    if(left!=-1 && right !=-1 && targetindex==-1)
	    {
	    	GrassCoverage leftGrassCoverage = (GrassCoverage) list.get(left);  
	    	GrassCoverage rightGrassCoverage = (GrassCoverage) list.get(right); 
	    	double sub = Double.parseDouble(rightGrassCoverage.getCoverage()) - Double.parseDouble(leftGrassCoverage.getCoverage());
	    	double cl = Double.parseDouble(rightGrassCoverage.getProduction()) - Double.parseDouble(leftGrassCoverage.getProduction());
	    	double subv = avgJs - Double.parseDouble(leftGrassCoverage.getCoverage());
	    	chanliang =((cl/sub)*subv) + Double.parseDouble(leftGrassCoverage.getProduction());//Math.abs(Math.floor(cl * (avgJs - sub)/(avgJs -Double.parseDouble(leftGrassCoverage.getCoverage())) + Double.parseDouble(leftGrassCoverage.getProduction())) );
	    }
	    else if(targetindex != -1)
	    {
	    	GrassCoverage grassCoverage = (GrassCoverage) list.get(targetindex);  
	    	chanliang = Double.parseDouble(grassCoverage.getProduction()) ;
	    }
	    else if(targetindex==-1 && right !=-1)
	    {
	    	GrassCoverage grassCoverage = (GrassCoverage) list.get(right);  
	    	chanliang =Math.abs(Math.floor( Double.parseDouble(grassCoverage.getProduction())/Double.parseDouble(grassCoverage.getCoverage())*avgJs)) ;
	    }
	    else if(targetindex==-1 && left !=-1)
	    {
	    	GrassCoverage grassCoverage = (GrassCoverage) list.get(left);  
	    	chanliang = Math.abs(Math.floor(Double.parseDouble(grassCoverage.getProduction())/Double.parseDouble(grassCoverage.getCoverage())*avgJs)) ;
	    }
	    
	    //根据系数计算当前月份草量
	    if(beltMonthxishu != curMonthxishu)
	    	chanliang = Math.floor((chanliang/beltMonthxishu)*curMonthxishu);
	    return chanliang;
	}
	
	public static void main(String arg[])
	{
		double value = Math.floor((10/0.298)*0.298);
		System.out.println(value);
	}
}